using System;
using System.Collections.Generic;
using System.Text;

namespace OpenComposite.EII.Designer.Workflow
{
	public static class CodeSnippets
	{
		#region Code Method DoHumanActivity
		public static string CodeMethodDoHumanActivity =
		"\t\tpublic FormSchema DoHumanActivity(int userKey, int idProcess, int idActivity, string targetURL, " + Environment.NewLine +
		"\t\t\tFormSchema input, string includedParticipants, string excludedParticipants)" + Environment.NewLine +
		"\t\t{" + Environment.NewLine +
		"\t\t    XmlDocument xd = new XmlDocument();" + Environment.NewLine +
		"\t\t    using (MemoryStream ms = new MemoryStream())" + Environment.NewLine +
		"\t\t    {" + Environment.NewLine +
		"\t\t        XmlSerializer xs = new XmlSerializer(typeof(FormSchema));" + Environment.NewLine +
		"\t\t        xs.Serialize(ms, input);" + Environment.NewLine +
		"\t\t        ms.Position = 0;" + Environment.NewLine +
		"\t\t        xd.Load(ms);" + Environment.NewLine +
		"\t\t    }" + Environment.NewLine +
		"\t\t    ActivityInstance ai = new ActivityInstance(idProcess, 0, idActivity, 0, Guid.Empty, 0, null);" + Environment.NewLine +
		"\t\t    ai.AdhocUserKey = userKey;" + Environment.NewLine +
		"\t\t    string userIds, roleIds, orgUnitIds;" + Environment.NewLine +
		"\t\t    // parse includes users, roles and orgunits" + Environment.NewLine +
		"\t\t    splitParticipants(includedParticipants, out userIds, out roleIds, out orgUnitIds);" + Environment.NewLine +
		"\t\t    ai.AdhocInclUsers = userIds;" + Environment.NewLine +
		"\t\t    ai.AdhocInclRoles = roleIds;" + Environment.NewLine +
		"\t\t    ai.AdhocInclOrgs = orgUnitIds;" + Environment.NewLine +
		"\t\t    // parse excludes users, roles and orgunits" + Environment.NewLine +
		"\t\t    splitParticipants(excludedParticipants, out userIds, out roleIds, out orgUnitIds);" + Environment.NewLine +
		"\t\t    ai.AdhocExclOrgs = userIds;" + Environment.NewLine +
		"\t\t    ai.AdhocExclRoles = roleIds;" + Environment.NewLine +
		"\t\t    ai.AdhocExclOrgs = orgUnitIds;" + Environment.NewLine +
		"\t\t    // create channel, set outgoing message headers and call human activity service" + Environment.NewLine +
		"\t\t    XmlNode xn = ai.SerializeToXmlNode();" + Environment.NewLine +
		"\t\t    SoapEnvelope env = new SoapEnvelope();" + Environment.NewLine +
		"\t\t    env.Body.AppendChild(env.ImportNode(xd.DocumentElement, true));" + Environment.NewLine +
		"\t\t    env.CreateHeader();" + Environment.NewLine +
		"\t\t    env.Header.AppendChild(env.ImportNode(xn, true));" + Environment.NewLine +
		"\t\t    env.Context.Addressing.Action = new Action(\"http://www.OpenComposite.com/HumanActivity/SetInputData\");" + Environment.NewLine +
		"\t\t    env.Context.Addressing.To = new To(new Uri(targetURL));" + Environment.NewLine +
		"\t\t    _msgID = env.Context.Addressing.MessageID;" + Environment.NewLine +
		"\t\t    SoapSender sender = new SoapSender(new Uri(targetURL));" + Environment.NewLine +
		"\t\t    _are = new AutoResetEvent(false);" + Environment.NewLine +
		"\t\t    EndpointReference ep = null;" + Environment.NewLine +
		"\t\t    int port = 31000;" + Environment.NewLine +
		"\t\t    try" + Environment.NewLine +
		"\t\t    {" + Environment.NewLine +
		"\t\t        bool notok = true;" + Environment.NewLine +
		"\t\t        while (notok)" + Environment.NewLine +
		"\t\t        {" + Environment.NewLine +
		"\t\t            try" + Environment.NewLine +
		"\t\t            {" + Environment.NewLine +
		"\t\t                ep = new EndpointReference(" + Environment.NewLine +
		"\t\t                    new Uri(string.Format(\"soap.tcp://{0}:{1}/HAResponse\"," + Environment.NewLine +
		"\t\t                        Environment.MachineName, port)));" + Environment.NewLine +
		"\t\t                SoapReceivers.Add(ep, this);" + Environment.NewLine +
		"\t\t                notok = false;" + Environment.NewLine +
		"\t\t                env.Context.Addressing.ReplyTo = new ReplyTo(" + Environment.NewLine +
		"\t\t                    new Uri(string.Format(\"soap.tcp://{0}:{1}/HAResponse\"," + Environment.NewLine +
		"\t\t                        Environment.MachineName, port)));" + Environment.NewLine +
		"\t\t            }" + Environment.NewLine +
		"\t\t            catch" + Environment.NewLine +
		"\t\t            {" + Environment.NewLine +
		"\t\t                notok = true;" + Environment.NewLine +
		"\t\t                port++;" + Environment.NewLine +
		"\t\t            }" + Environment.NewLine +
		"\t\t        }" + Environment.NewLine +
		"\t\t        sender.Send(env);" + Environment.NewLine +
		"\t\t        _are.WaitOne();" + Environment.NewLine +
		"\t\t    }" + Environment.NewLine +
		"\t\t    catch (Exception ex)" + Environment.NewLine +
		"\t\t    {" + Environment.NewLine +
		"\t\t        System.Diagnostics.Debug.WriteLine(ex.Message);" + Environment.NewLine +
		"\t\t    }" + Environment.NewLine +
		"\t\t    finally" + Environment.NewLine +
		"\t\t    {" + Environment.NewLine +
		"\t\t        if (ep != null && SoapReceivers.Contains(ep))" + Environment.NewLine +
		"\t\t            SoapReceivers.Remove(ep);" + Environment.NewLine +
		"\t\t    }" + Environment.NewLine +
		"\t\t    return _result;" + Environment.NewLine +
		"\t\t}";
		#endregion

		#region Code Method ProcessMessage
		public static string CodeMethodProcessMessage =
		"\t\tpublic override void ProcessMessage(SoapEnvelope message)" + Environment.NewLine +
		"\t\t{" + Environment.NewLine +
		"\t\t\tbase.ProcessMessage(message);" + Environment.NewLine +
		"\t\t\tif ( message.Context.Addressing.RelatesTo.Value.Equals(_msgID.Value) ) {" + Environment.NewLine +
		"\t\t\t\tusing ( MemoryStream ms = new MemoryStream() )" + Environment.NewLine +
		"\t\t\t\tusing ( XmlWriter xw = XmlWriter.Create(ms) ) {" + Environment.NewLine +
		"\t\t\t\t\tmessage.Body.WriteContentTo(xw);" + Environment.NewLine +
		"\t\t\t\t\txw.Flush();" + Environment.NewLine +
		"\t\t\t\t\tms.Position = 0;" + Environment.NewLine +
		"\t\t\t\t\tXmlSerializer xs = new XmlSerializer(typeof(FormSchema));" + Environment.NewLine +
		"\t\t\t\t\t_result = xs.Deserialize(ms) as FormSchema;" + Environment.NewLine +
		"\t\t\t\t}" + Environment.NewLine +
		"\t\t\t\t_are.Set();" + Environment.NewLine +
		"\t\t\t}" + Environment.NewLine +
		"\t\t}";
		#endregion

		#region Code Method workflow_ReplyEvent
		public static string CodeMethodworkflow_ReplyEvent =
		"\tprivate void workflow_ReplyEvent(object sender, %WF_STARTMETHODNAME%_ReplyEventArgs e) " + Environment.NewLine +
		"\t{" + Environment.NewLine +
		"\t\t_results.Add(e.InstanceId, e.Output);" + Environment.NewLine +
		"\t\tif ( _waitHandles.ContainsKey(e.InstanceId) ) {" + Environment.NewLine +
		"\t\t\t_waitHandles[e.InstanceId].Set();" + Environment.NewLine +
		"\t\t}" + Environment.NewLine +
		"\t}";
		#endregion

		#region Code Line bind workflow_ReplyEvent
		public static string CodeLineBindWorkflow_ReplyEvent =
			"_localService.%WF_STARTMETHODNAME%_ReplyEvent += " +
			"new %WF_STARTMETHODNAME%_ReplyEventHandler(workflow_ReplyEvent);";
		#endregion

		#region Code Method splitParticipants
		public static string CodeMethod_splitParticipants =
@"		private void splitParticipants(
			string participants,
			ParticipationType participationType,
			ref List<ParticipantItem> listParticipants)
		{
			if ( listParticipants == null ) listParticipants = new List<ParticipantItem>();
			string[] partArray = participants.Split(';');
			if ( partArray != null ) {
				foreach ( string part in partArray ) {
					string[] typeidname = part.Split(':');
					if ( typeidname != null && typeidname.Length >= 2 ) {
						int id;
						if ( !int.TryParse(typeidname[1], out id) ) continue;
						switch ( typeidname[0] ) {
							case ""u"":
								listParticipants.Add(
									new ParticipantItem(ParticipantType.User, id, participationType)); 
								break;
							case ""r"":
								listParticipants.Add(
									new ParticipantItem(ParticipantType.Role, id, participationType));
								break;
							case ""o"":
								listParticipants.Add(
									new ParticipantItem(ParticipantType.Organization, id, participationType));
								break;
							case ""ou"":
								listParticipants.Add(
									new ParticipantItem(ParticipantType.OrganizationUnit, id, participationType));
								break;
						}
					}
				}
			}
		}
";

		//"\t\tprivate void splitParticipants(string participants," + Environment.NewLine +
		//"\t\t\tout List<ParticipantItem> participants)" + Environment.NewLine +
		//"\t\t{" + Environment.NewLine +
		//"\t\t\tparticipants = null;" + Environment.NewLine +
		//"\t\t\tstring[] partArray = participants.Split(';');" + Environment.NewLine +
		//"\t\t\tif ( partArray != null ) {" + Environment.NewLine +
		//"\t\t\t\tparticipants = new List<ParticipantItem>();" + Environment.NewLine +
		//"\t\t\t\tforeach ( string part in partArray ) {" + Environment.NewLine +
		//"\t\t\t\t\tstring[] typeidname = part.Split(':');" + Environment.NewLine +
		//"\t\t\t\t\tif ( typeidname != null && typeidname.Length >= 2 ) {" + Environment.NewLine +
		//"\t\t\t\t\t\tswitch ( typeidname[0] ) {" + Environment.NewLine +
		//"\t\t\t\t\t\t\tcase \"u\":" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tif ( userIds != \"\" ) userIds += \",\";" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tuserIds += typeidname[1];" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tbreak;" + Environment.NewLine +
		//"\t\t\t\t\t\t\tcase \"r\":" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tif ( roleIds != \"\" ) roleIds += \",\";" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\troleIds += typeidname[1];" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tbreak;" + Environment.NewLine +
		//"\t\t\t\t\t\t\tcase \"o\":" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tbreak;" + Environment.NewLine +
		//"\t\t\t\t\t\t\tcase \"ou\":" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tif ( orgUnitIds != \"\" ) orgUnitIds += \",\";" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\torgUnitIds += typeidname[1];" + Environment.NewLine +
		//"\t\t\t\t\t\t\t\tbreak;" + Environment.NewLine +
		//"\t\t\t\t\t\t}" + Environment.NewLine +
		//"\t\t\t\t\t}" + Environment.NewLine +
		//"\t\t\t\t}" + Environment.NewLine +
		//"\t\t\t}" + Environment.NewLine +
		//"\t\t}" + Environment.NewLine;
		#endregion
	}
}
